import {add, get,updateOne,delOne} from "../../until/db"
Page({
  data:{
    list:[],
    name:"",		//菜谱名称
		id:"",	
    toAdd:false,
    modify:false
  },
  async onLoad(){
    var result = await get("class")
    // console.log(result);
    this.setData({
      list:result.data
    })
  },

  //添加输入框显示
  showAdd(){
    this.setData({
      toAdd:true
    })
  },
  inputs(e){
    // console.log(e);
    var name= e.detail.value;
    this.data.name = name
  },

  //添加数据
  async doAdd(){
    //把数据添加到数据库
    var result = await add("class",{name:this.data.name}).catch(err=>{
      console.log(err); 
    })
    var result = await get("class")
    // console.log(result);
    this.setData({
      list:result.data,
      toAdd:false
    })
  },

  //修改数据
  showUpdate(e){
    // console.log(e);
    var id = e.currentTarget.id;
    var name = e.currentTarget.dataset.name
    this.data.id = id
    this.setData({
      name,
      modify:true  
    })
  },
  async change(){
    var result = updateOne("class",this.data.id,{name:this.data.name}).catch(err=>{
      console.log(err);})
    // console.log(result);
    var result = await get("class")
    // console.log(result);
    this.setData({
      list:result.data,
      modify:false
    })
  },

  //删除
  showDel(e){
    var id = e.currentTarget.id;
    wx.showModal({
      title:"温馨提示",
      content:"确定删除吗？",
      success: async res=>{
        if(res.confirm){
          var result = await delOne("class",id).catch(err=>{
            console.log(err);})
          var result = await get("class")
          this.setData({
            list:result.data
          })
        }
      }
    })
  }
})